package org.lex.keytool;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.UIManager;

import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.lex.keytool.ui.MainContentPane;

public class KeyTool extends SingleFrameApplication {
	public static void main(String[] args) {
		UIManager.put("swing.boldMetal", Boolean.FALSE);
		Application.launch(KeyTool.class, args);
	}

	public static KeyTool getInstance() {
		return Application.getInstance(KeyTool.class);
	}

	private static final String SESSION_FILE = "session.xml";

	@Override
	protected void startup() {
		JFrame frame = this.getMainFrame();
		frame.setName("mainFrame");
		frame.setTitle("Key Tool");
		frame.setContentPane(new MainContentPane());
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				KeyTool.this.exit(e);
			}
		});
		try {
			this.getContext().getSessionStorage().restore(frame, SESSION_FILE);
		} catch (IOException e) {
			e.printStackTrace();
		}

		frame.setVisible(true);
	}

	@Override
	protected void shutdown() {
		try {
			this.getContext().getSessionStorage().save(getMainFrame(), SESSION_FILE);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
